﻿/**
 * 
 * Copyright 2009, 2010 Mike Jamer [email: mikejamer@gmail.com ]
 * 
 * This File is part of Moose IRC
 * 
 * MooseIRC is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * MooseIRC is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with MooseIRC.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace moose.irc
{

    /// <summary>
    /// IRC User Collection, is a collection for containing users,
    /// but has all the management capabilities we need.
    /// 
    /// This should include the ability to keep users up to date.
    /// for things like nickname updates, or for times when we need to update
    /// a users username or hostname.
    /// </summary>
    /// <remarks>
    /// Alot of this functionality was originally indended to be included within the IRC User class, 
    /// but this will allow us to have locallized lists.
    /// So we can have client specific lists, or even use this for channel lists.
    /// </remarks>
    internal class IrcUserCollection
    {

        /// <summary>
        /// Hash table of users
        /// [nickname, User]
        /// </summary>
        private Dictionary<string, IrcUser> m_dictUsers;

        /// <summary>
        /// Initialize IRC User Collection
        /// </summary>
        public IrcUserCollection()
            : base()
        {
            m_dictUsers = new Dictionary<string, IrcUser>();
            return;
        }

        /// <summary>
        /// Get a user, based on user string
        /// </summary>
        /// <param name="user_string">user string representing user</param>
        public IrcUser get_user(string user_string)
        {
            string nick = get_nickname(user_string);
            if (m_dictUsers.ContainsKey(nick))
            {
                IrcUser usr = m_dictUsers[nick];
                update_user(usr, user_string);
                return usr;
            }

            m_dictUsers[nick] = new IrcUser(user_string);
            return m_dictUsers[nick];
        }

        /// <summary>
        /// Enumerate all users
        /// </summary>
        /// <returns>Enumerates all users</returns>
        public IEnumerable<IrcUser> get_all()
        {
            return m_dictUsers.Values;
        }

        /// <summary>
        /// Returns the nickname portion of a user string
        /// </summary>
        /// <param name="user_string">user string to parse nickname out of</param>
        /// <returns>nickname</returns>
        private string get_nickname(string user_string)
        {
            if (user_string.Contains('!') && user_string.Contains('@'))
            {
                string[] name_tok = user_string.Split(new char[] { '!', '@' }, StringSplitOptions.RemoveEmptyEntries);
                return name_tok[0];
            }
            return user_string;
        }

        /// <summary>
        /// Updates users details
        /// </summary>
        /// <param name="user"></param>
        /// <param name="user_string"></param>
        private void update_user(IrcUser user, string user_string)
        {
            IrcUser usr = new IrcUser(user_string);
            if (usr.Nickname != user.Nickname)
            { //If the nickname has been updated, clean that up
                m_dictUsers[usr.Nickname] = user;
                m_dictUsers.Remove(user.Nickname);
            }
            user.Nickname = usr.Nickname;
            user.Username = usr.Username;
            user.Hostname = usr.Hostname;

            return;
        }

        /// <summary>
        /// Remove a user
        /// </summary>
        /// <param name="nick">nickname of user to remove</param>
        public void remove_user(string nick)
        {
            m_dictUsers.Remove(nick);
            return;
        }

        /// <summary>
        /// Remove all users
        /// </summary>
        public void remove_all()
        {
            m_dictUsers.Clear();
            return;
        }

    }
}
